#ifndef MODBUSTEST_H
#define MODBUSTEST_H
#include <QObject>
#include <QString>
#include <QByteArray>
#include <QMetaEnum>
#include <QtEndian>
#include "devicelist.h"
#include "hj212.h"
namespace Moolink {

    class ModbusTest: public QObject
    {
        Q_OBJECT

    public:

        enum protocolList{
            Error=0,
            RTU,
            ASCII,
            CODmax,
        };


        Q_ENUM(protocolList)

        static QByteArray getReadCmd(Device *device,int funCode);
        //static QString getDataResultStr(QString protocol,int commAddr,QString recvData);
        static QString getDataResultStr(Device *device,QByteArray responseData);

        static quint16 calculateCRC(const char *data, qint32 len);
        static quint8 calculateLRC(const char *data, qint32 len);

    private:
        inline static quint16 crc_reflect(quint16 data, qint32 len)
        {
            // Generated by pycrc v0.8.3, https://pycrc.org
            // Width = 16, Poly = 0x8005, XorIn = 0xffff, ReflectIn = True,
            // XorOut = 0x0000, ReflectOut = True, Algorithm = bit-by-bit-fast

            quint16 ret = data & 0x01;
            for (qint32 i = 1; i < len; i++) {
                data >>= 1;
                ret = (ret << 1) | (data & 0x01);
            }
            return ret;
        }

    };
}
#endif // MODBUSTEST_H
